package com.coffee.service.impl;

import com.coffee.constant.MessageConstant;
import com.coffee.constant.StatusConstant;
import com.coffee.dto.SetmealDTO;
import com.coffee.dto.SetmealPageQueryDTO;
import com.coffee.entity.Dish;
import com.coffee.entity.Setmeal;
import com.coffee.entity.SetmealDish;
import com.coffee.exception.DeletionNotAllowedException;
import com.coffee.mapper.CategoryMapper;
import com.coffee.mapper.SetmealDishMapper;
import com.coffee.mapper.SetmealMapper;
import com.coffee.result.PageResult;
import com.coffee.service.SetmealService;
import com.coffee.vo.DishItemVO;
import com.coffee.vo.SetmealVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional
    public void insert(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //套餐表插入信息
        setmealMapper.insert(setmeal);
        //获取insert语句得到的主键值
        Long setmealId=setmeal.getId();
        //向套餐菜品关系表插入多条数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes!=null&&setmealDishes.size()>0){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setId(setmealId);
            });
            //插入多条数据
            setmealDishMapper.insertBatch(setmealDishes);
        }

    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    public SetmealVO getById(Long id) {
        //查询setmeal表
        Setmeal setmeal=setmealMapper.getById(id);
        //属性拷贝
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        //其他属性赋值
        setmealVO.setCategoryName(categoryMapper.getNameByCategpryid(setmeal.getCategoryId()));
        List<SetmealDish> setmealDishs = setmealDishMapper.getSetmealDishsBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishs);

        return setmealVO;
    }

    /**
     * 批量删除套餐
     * @param setmealIds
     */
    @Transactional
    public void delete(List<Long> setmealIds) {
        //判断要删除的套餐是否有在起售中的，起售中的套餐不能删除
        for (Long setmealId:setmealIds){
            Setmeal setmeal=setmealMapper.getById(setmealId);
            if (setmeal.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //批量删除1.删除套餐菜品关联表中的数据，2.删除套餐表中的数据
        setmealDishMapper.deleteBySetmealIds(setmealIds);
        setmealMapper.deleteByIds(setmealIds);

    }

    /**
     * 修改套餐信息
     * @param setmealDTO
     */
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //1.修改套餐表
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //得到套餐id
        Long id=setmealDTO.getId();
        //2.修改套餐菜品关联表,先删除，再插入
        setmealDishMapper.deleteBySetmealId(setmeal.getId());
        List<SetmealDish> setmealDishs=setmealDTO.getSetmealDishes();
        //给套餐菜品关系赋值套餐id
        // 不明白，不是本来就有套餐id吗，为什么还要修改
        setmealDishs.forEach(setmealDish -> {
            setmealDish.setSetmealId(id);
        });
        setmealDishMapper.insertBatch(setmealDishs);

    }

    /**
     * 套餐起售停售
     * @param setmealId
     * @param status
     */
    public void startOrStop(Long setmealId, Integer status) {
        //若是起售请求，需要判断套餐包含的菜品是否有停售状态的，若有则不允许套餐起售
        if (status==StatusConstant.ENABLE){
            //获取当前套餐包含的菜品
            List<SetmealDish> setmealDishes=setmealDishMapper.getSetmealDishsBySetmealId(setmealId);
            setmealDishes.forEach(setmealDish -> {
                Dish dish=new Dish();
                dish.setId(setmealDish.getDishId());
                if (dish.getStatus()==StatusConstant.DISABLE){
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        Setmeal setmeal=new Setmeal();
        setmeal.setId(setmealId);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
